package com.hyronjs.jiangbiao.antrus.controller;

import java.lang.reflect.Field;

import com.hyronjs.jiangbiao.antrus.forms.Form;
import com.hyronjs.jiangbiao.antrus.log.Logger;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;

/**
 * 实现对Active的包装，包装成控制器
 * 
 * @author jiangbiao
 * 
 */
public class BaseController extends Activity {

	private static final String TAG = "BaseController";
	public static String packageName;
	private int mainView;
	private MenuItem selectedItem = null;
	// 默认情况下XML画面ID应该和控制器同名
	private String XMLName = this.getClass().getName().toLowerCase();

	private static final int DIALOG_ID_PROGRESS_DEFAULT = 0x174980;

	@Override
	protected final void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);

		onBeforeCreate(savedInstanceState);
		super.onCreate(savedInstanceState);

		onCreateContent(savedInstanceState);
		onAfterCreate(savedInstanceState);
	}

	/**
	 * 画面初期化之前的处理
	 * 
	 * @param savedInstanceState
	 */
	protected void onBeforeCreate(Bundle savedInstanceState) {
	}

	/**
	 * 画面初期化之后的处理
	 * 
	 * @param savedInstanceState
	 */
	protected void onAfterCreate(Bundle savedInstanceState) {
		addViewAction();
	}

	/**
	 * 给按钮增加事件
	 */
	public void addViewAction(){
		
	}
	
	/**
	 * 实现画面的跳转
	 * @next 跳转目的地
	 * @param 参数Form
	 */
	public void dropToNextController(BaseController next, Form param){
		Intent intent = new Intent();
		Bundle bundle = new Bundle();
		bundle.putSerializable("param", param);
		intent.putExtras(bundle);
		intent.setClass(this, next.getClass());
		startActivity(intent);
	}
	
	
	/**
	 * 画面的初期化
	 * 
	 * @param savedInstanceState
	 * @throws ClassNotFoundException
	 * @throws NoSuchFieldException
	 * @throws SecurityException
	 * @throws IllegalAccessException
	 * @throws IllegalArgumentException
	 */
	protected void onCreateContent(Bundle savedInstanceState) {
		try {
			PackageManager manager = this.getPackageManager();
			PackageInfo info = manager.getPackageInfo(this.getPackageName(), 0);
			packageName = info.packageName;  //包名
			Class<?> c = Class.forName( packageName + ".R$layout");
			Field f;
			XMLName = XMLName.substring(XMLName.lastIndexOf(".") + 1).replace("controller", "");
			f = c.getDeclaredField(XMLName);
			mainView = f.getInt(null);
			setContentView(mainView);
		} catch (Exception e) {
			Logger.i(TAG, "onCreateContent error : " + e.getMessage());
		}
	}

	protected int getContentViewID() {
		return -1;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		if (hasCustomOptionsMenu()) {
			return createCustomOptionsMenu(menu);
		}
		return createDefaultOptionsMenu(menu);
	}

	@Override
	public final boolean onOptionsItemSelected(MenuItem item) {
		int itemId = item.getItemId();

		System.out.println("Context Item Selected... " + item.getTitle());

		boolean result = onOptionsItemSelected(itemId);
		if (result) {
			if (selectedItem != null) {
				selectedItem
						.setIcon(optionMenuIds[selectedItem.getItemId()][1]);
			}
			item.setIcon(optionMenuIds[itemId][2]);
			selectedItem = item;
		}

		return true;
	}

	protected final void deselectAllOptionMenuItems() {
		if (selectedItem != null) {
			selectedItem.setIcon(optionMenuIds[selectedItem.getItemId()][1]);
			selectedItem = null;
		}
	}

	protected boolean onOptionsItemSelected(int itemId) {
		return true;
	}

	private int[][] optionMenuIds = {
	/*
	 * { R.string.title_of_item_01, R.drawable.icon_deselected_item_01,
	 * R.drawable.icon_selected_item_01 }
	 */
	};

	private boolean createDefaultOptionsMenu(Menu menu) {
		MenuItem item;

		for (int i = 0; i < optionMenuIds.length; i++) {
			item = menu.add(0, i, i, optionMenuIds[i][0]);
			item.setIcon(optionMenuIds[i][1]);
		}

		return true;
	}

	protected boolean hasCustomOptionsMenu() {
		return false;
	}

	protected boolean createCustomOptionsMenu(Menu menu) {
		return false;
	}

	public void onError(Form response) {
	}

	public void onSuccess(Form response) {
	}

	public void preProcessData(Form response) {
		Logger.i(TAG, "LoginActivity//preProcessData");

		Logger.i(TAG, "Response: " + response);
		Logger.i(TAG, "Activity ID: " + response.getTargetActivityID());
		Logger.i(TAG, "Tag: " + response.getTag());
	}

	public void processData(Form response) {
		Logger.i(TAG, "LoginActivity//processData");

		Logger.i(TAG, "Response: " + response);
		Logger.i(TAG, "Activity ID: " + response.getTargetActivityID());
		Logger.i(TAG, "Tag: " + response.getTag());
	}

	protected void showProgress() {
		showDialog(DIALOG_ID_PROGRESS_DEFAULT);
	}

	protected void hideProgress() {
		try {
			removeDialog(DIALOG_ID_PROGRESS_DEFAULT);
		} catch (IllegalArgumentException iae) {
		}
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case DIALOG_ID_PROGRESS_DEFAULT:
			ProgressDialog dlg = new ProgressDialog(this);
			dlg.setMessage("Working...");
			dlg.setCancelable(true);
			return dlg;
		default:
			return super.onCreateDialog(id);

		}
	}

}
